package com.zhs.gccs.activity.gccs;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.KeyboardUtils;
import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;
import com.umeng.commonsdk.debug.E;
import com.zhs.gccs.R;
import com.zhs.gccs.base.BaseActivity;
import com.zhs.gccs.http.Api;
import com.zhs.gccs.http.ProgressObserver;
import com.zhs.gccs.model.GccsProjectSsgh;
import com.zhs.gccs.utils.OnNoDoubleClickListener;
import com.zhs.gccs.utils.ToastUtils;
import com.zhs.gccs.vo.ApplyGccsTask;
import com.zhs.gccs.vo.GccsDataExtAddBean;
import com.zhs.gccs.vo.GccsExtDataBean;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.RequestBody;

public class GccsXmsjFormActivity extends BaseActivity {
    public static String intent_data = "data";
    public static String intent_dataExt ="dataExt";
    @BindView(R.id.iv_back)
    View iv_back;
    @BindView(R.id.btn_back)
    View btn_back;
    @BindView(R.id.tv_save)
    View tv_save;
    @BindView(R.id.ll_root)
    LinearLayout llRoot;

    @BindView(R.id.radio_1)
    RadioGroup radio_1;//是否移交
    @BindView(R.id.radio_2)
    RadioGroup radio_2;
    @BindView(R.id.radio_3)
    RadioGroup radio_3;
    @BindView(R.id.radio_4)
    RadioGroup radio_4;
    @BindView(R.id.radio_5)
    RadioGroup radio_5;
    @BindView(R.id.radio_6)
    RadioGroup radio_6;
    @BindView(R.id.radio_7)
    RadioGroup radio_7;
    @BindView(R.id.radio_8)
    RadioGroup radio_8;

    ApplyGccsTask taskBean;
    GccsProjectSsgh gccsProjectSsgh;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gccs_xmsj_form);
//        ImmersionBar.with(this).transparentBar().statusBarDarkFont(false).init();
//        ImmersionBar.with(this)
//                .statusBarColor(R.color.main_title_pressed_bg)
//                .statusBarDarkFont(false).init();
        ImmersionBar.with(this).transparentBar().statusBarDarkFont(false).init();
        ButterKnife.bind(this);
        llRoot.setPadding(0, ImmersionBar.getStatusBarHeight(this), 0, 0);
        Intent intent = getIntent();
        if(intent.hasExtra(intent_data)){
            taskBean = (ApplyGccsTask) intent.getExtras().get(intent_data);
        }
        initView();
        if(taskBean!=null){
            getGccsFormData();
        }
    }

    private void initView() {
        iv_back.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                KeyboardUtils.hideSoftInput(GccsXmsjFormActivity.this);
                finish();
            }
        });
        btn_back.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                KeyboardUtils.hideSoftInput(GccsXmsjFormActivity.this);
                finish();
            }
        });
        tv_save.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                saveData();
            }
        });
    }

    private void getGccsFormData() {
        showDialog();
        toSubscribeSingleOb(Api.getDefault().
                        getGccsExtForm(taskBean.getPid()),
                new ProgressObserver(this, false) {
                    @Override
                    protected void _onNext(String result) {
                        dismissDialog();
                        GccsExtDataBean bean = new Gson().fromJson(result, GccsExtDataBean.class);//GsonImpl.get().toObject(result, SmTaskBean.class);
                        if (bean != null && bean.getCode() == 200
                                && bean.getData() != null) {
                            gccsProjectSsgh = bean.getData();
                            initData();
                        } else {

                        }
                    }

                    @Override
                    protected void _onError(String result) {
                        dismissDialog();
                    }
                });
    }

    private void initData() {
        if(gccsProjectSsgh==null)return;
        if("1".equals(gccsProjectSsgh.getSjjlPksj())){
            radio_1.check(R.id.radio_1_1);
        }else if("0".equals(gccsProjectSsgh.getSjjlPksj())){
            radio_1.check(R.id.radio_1_2);
        }
        if("1".equals(gccsProjectSsgh.getSjjlWzjyj())){
            radio_2.check(R.id.radio_2_1);
        }else if("0".equals(gccsProjectSsgh.getSjjlWzjyj())){
            radio_2.check(R.id.radio_2_2);
        }
        if("1".equals(gccsProjectSsgh.getSjjlQsztsjt())){
            radio_3.check(R.id.radio_3_1);
        }else if("0".equals(gccsProjectSsgh.getSjjlQsztsjt())){
            radio_3.check(R.id.radio_3_2);
        }
        if("1".equals(gccsProjectSsgh.getSjjlQsgcsjfbt())){
            radio_4.check(R.id.radio_4_1);
        }else if("0".equals(gccsProjectSsgh.getSjjlQsgcsjfbt())){
            radio_4.check(R.id.radio_4_2);
        }
        if("1".equals(gccsProjectSsgh.getSjjlQszydtgct())){
            radio_5.check(R.id.radio_5_1);
        }else if("0".equals(gccsProjectSsgh.getSjjlQszydtgct())){
            radio_5.check(R.id.radio_5_2);
        }
        if("1".equals(gccsProjectSsgh.getSjjlJlrzzj())){
            radio_6.check(R.id.radio_6_1);
        }else if("0".equals(gccsProjectSsgh.getSjjlJlrzzj())){
            radio_6.check(R.id.radio_6_2);
        }
        if("1".equals(gccsProjectSsgh.getSjjlWxs())){
            radio_7.check(R.id.radio_7_1);
        }else if("0".equals(gccsProjectSsgh.getSjjlWxs())){
            radio_7.check(R.id.radio_7_2);
        }
        if("1".equals(gccsProjectSsgh.getSjjlJljsct())){
            radio_8.check(R.id.radio_8_1);
        }else if("0".equals(gccsProjectSsgh.getSjjlJljsct())){
            radio_8.check(R.id.radio_8_2);
        }
    }

    private void saveData() {
        KeyboardUtils.hideSoftInput(this);
        showDialog();
        Map hashMap = new HashMap<>();
        hashMap.put("pid",taskBean.getPid());
        if (radio_1.getCheckedRadioButtonId() == R.id.radio_1_1) {
            hashMap.put("sjjlPksj","1");
        } else if (radio_1.getCheckedRadioButtonId() == R.id.radio_1_2){
            hashMap.put("sjjlPksj","0");
        }
        if (radio_2.getCheckedRadioButtonId() == R.id.radio_2_1) {
            hashMap.put("sjjlWzjyj","1");
        } else if (radio_2.getCheckedRadioButtonId() == R.id.radio_2_2){
            hashMap.put("sjjlWzjyj","0");
        }
        if (radio_3.getCheckedRadioButtonId() == R.id.radio_3_1) {
            hashMap.put("sjjlQsztsjt","1");
        } else if (radio_3.getCheckedRadioButtonId() == R.id.radio_3_2){
            hashMap.put("sjjlQsztsjt","0");
        }
        if (radio_4.getCheckedRadioButtonId() == R.id.radio_4_1) {
            hashMap.put("sjjlQsgcsjfbt","1");
        } else if (radio_4.getCheckedRadioButtonId() == R.id.radio_4_2){
            hashMap.put("sjjlQsgcsjfbt","0");
        }
        if (radio_5.getCheckedRadioButtonId() == R.id.radio_5_1) {
            hashMap.put("sjjlQszydtgct","1");
        } else if (radio_5.getCheckedRadioButtonId() == R.id.radio_5_2){
            hashMap.put("sjjlQszydtgct","0");
        }
        if (radio_6.getCheckedRadioButtonId() == R.id.radio_6_1) {
            hashMap.put("sjjlJlrzzj","1");
        } else if (radio_6.getCheckedRadioButtonId() == R.id.radio_6_2){
            hashMap.put("sjjlJlrzzj","0");
        }
        if (radio_7.getCheckedRadioButtonId() == R.id.radio_7_1) {
            hashMap.put("sjjlWxs","1");
        } else if (radio_7.getCheckedRadioButtonId() == R.id.radio_7_2){
            hashMap.put("sjjlWxs","0");
        }
        if (radio_8.getCheckedRadioButtonId() == R.id.radio_8_1) {
            hashMap.put("sjjlJljsct","1");
        } else if (radio_8.getCheckedRadioButtonId() == R.id.radio_8_2){
            hashMap.put("sjjlJljsct","0");
        }
        RequestBody requestBody = createRequestBody(hashMap);
        toSubscribeSingleOb(Api.getDefault().saveGccsExtform(requestBody),
                new ProgressObserver(this, false) {
                    @Override
                    protected void _onNext(String result) {
                        GccsDataExtAddBean bean = new Gson().
                                fromJson(result, GccsDataExtAddBean.class);
                        if (bean != null && bean.getCode() == 200) {
                            ToastUtils.toast("添加成功");
//                            setResult(RESULT_OK);
//                            finish();
                        }else {
                            ToastUtils.toast("添加失败，"+bean.getMsg());
                        }
                        dismissDialog();
                    }
                    @Override
                    protected void _onError(String result) {
                        ToastUtils.toast("添加失败");
                        dismissDialog();
                    }
                });
    }


}
